﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using mshtml;
using System.Xml;

namespace ResxManager
{
	class Translate
	{
		public string TranslateText(string input, string from, string to)
		{
			switch (from)
			{
				case "ja":
					from = "ja";
					break;
				case "zh-cn":
					from = "zh-CN";
					break;
				case "en":
					from = "en";
					break;
				default:
					from = "ko";
					break;
			}
			switch (to)
			{
				case "ja":
					to = "ja";
					break;
				case "zh-cn":
					to = "zh-CN";
					break;
				case "en":
					to = "en";
					break;
				default:
					to = "ko";
					break;
			}
			string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, from + "|" + to);

			WebRequest request = WebRequest.Create(url);
			request.Credentials = CredentialCache.DefaultCredentials;

			HttpWebResponse response = (HttpWebResponse)request.GetResponse();
			Encoding enc;

			enc = Encoding.GetEncoding(response.CharacterSet);

			Stream dataStream = response.GetResponseStream();
			StreamReader reader = new StreamReader(dataStream, enc);

			string responseFromServer = reader.ReadToEnd();

			HTMLDocument doc = new HTMLDocument();
			doc.designMode = "on";
			object[] oPageText = { responseFromServer };
			IHTMLDocument2 oMyDoc = (IHTMLDocument2)doc;
			oMyDoc.write(oPageText);

			IHTMLElement elm = doc.getElementById("result_box");
			return elm.innerText;
		}
		
	}
}
